package chapter1_4_join;

/**
 * 主线程 调用线程A 的join 方法后会被阻塞， 当其他线程调用了线程主线程 的
 * interrupt（）方法中断了线程主线程 时，主线程 会抛出InterruptedException 异常而返回。
 *
 * @author huayuhao
 * @date 2020/12/15 16:25
 */
public class Join2Test {

    public static void main(String[] args) throws InterruptedException {

        Thread A = new Thread(() -> {
            System.out.println("线程A 开始执行");
            for (; ; ) {

            }
        });

        Thread mainThread = Thread.currentThread();

        Thread B = new Thread(() -> {
            try {
                System.out.println("B 开始睡1s");
                Thread.sleep(1000);
                System.out.println("B 醒了!");
                mainThread.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        A.start();
        B.start();

        try {
            A.join();
        } catch (InterruptedException ex) {
            System.out.println("main Thread : " + ex);
        }
    }

}
